package com.netty.study.handler;

import io.netty.buffer.ByteBuf;
import io.netty.channel.ChannelHandlerContext;
import io.netty.handler.codec.MessageToByteEncoder;

public class StringToByteEncoderHandler extends MessageToByteEncoder<String> {
    @Override
    protected void encode(ChannelHandlerContext channelHandlerContext, String str, ByteBuf out) throws Exception {
        byte[] bytes = str.getBytes();
        /*先写入长度，然后写入内容*/
        out.writeInt(bytes.length);
        out.writeBytes(bytes);

    }
}